package com.pokermind.repository.mapper;

import com.pokermind.model.po.PokerHandAction;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * PokerHandAction 行映射器
 * 用于将ResultSet映射为PokerHandAction实体
 * 
 * @author PokerMind Team
 */
public class PokerHandActionRowMapper implements RowMapper<PokerHandAction> {
    
    @Override
    public PokerHandAction mapRow(ResultSet rs, int rowNum) throws SQLException {
        PokerHandAction action = new PokerHandAction();
        
        // 基本字段
        action.setId(rs.getLong("id"));
        action.setHandId(rs.getLong("hand_id"));
        action.setSeatNumber(rs.getInt("seat_number"));
        
        // 双字段玩家标识
        action.setPlayerId(rs.getString("player_id"));   // 前端业务ID（String）
        action.setUserId(getLongOrNull(rs, "user_id"));  // 数据库用户ID（Long）
        
        // 序列信息
        action.setStreet(rs.getString("street"));
        action.setActionSequence(rs.getInt("action_sequence"));
        action.setGlobalSequence(rs.getInt("global_sequence"));
        
        // 行动信息
        action.setActionType(rs.getString("action_type"));
        action.setAmount(rs.getBigDecimal("amount"));
        
        // 状态信息 - V4.7: 使用实际的数据库列名 stack/pot
        action.setStackAfter(rs.getBigDecimal("stack"));
        action.setPotAfter(rs.getBigDecimal("pot"));
        
        // 可选字段
        action.setThinkTimeSeconds(getIntegerOrNull(rs, "think_time_seconds"));
        
        // 时间字段 - 使用Long时间戳
        action.setCreateTime(getLongOrNull(rs, "create_time"));
        action.setUpdateTime(getLongOrNull(rs, "update_time"));
        
        return action;
    }
    
    /**
     * 安全获取Integer值，处理NULL情况
     */
    private Integer getIntegerOrNull(ResultSet rs, String columnName) throws SQLException {
        int value = rs.getInt(columnName);
        return rs.wasNull() ? null : value;
    }
    
    /**
     * 安全获取Long值，处理NULL情况
     */
    private Long getLongOrNull(ResultSet rs, String columnName) throws SQLException {
        long value = rs.getLong(columnName);
        return rs.wasNull() ? null : value;
    }
}